package com.wd.live.ui.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.wd.live.BaseActivity
import com.wd.live.R
import com.wd.live.databinding.ActivityMainBinding
import com.wd.live.tool.longToast
import com.wd.live.tool.lunchActivity
import com.wd.live.tool.viewBinding
import com.wd.live.ui.fragment.NearFragment
import com.wd.live.ui.fragment.PlazaFragment
import com.wd.live.ui.fragment.PrepareFragment
import com.wd.live.viewmodel.MainViewModel


/**
 * 首页
 */
private const val NUM_PAGES = 5

class MainActivity : BaseActivity() {
    private var mLocationListener: AMapLocationListener? = null
    private var mLocationClient: AMapLocationClient? = null
    private val binding: ActivityMainBinding by viewBinding()
    private val viewModel: MainViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.tabBg.setOnClickListener {

        }
        binding.viewPage.apply {
            adapter = ScreenSlidePagerAdapter(supportFragmentManager, lifecycle)
            isUserInputEnabled = false
        }

        binding.radioGroup.apply {
            check(R.id.tab_one)
            setOnCheckedChangeListener { group, checkedId ->
                when (checkedId) {
                    R.id.tab_one ->
                        binding.viewPage.currentItem = 0

                    R.id.tab_two ->
                        binding.viewPage.currentItem = 1

                    R.id.tab_three ->
                        binding.viewPage.currentItem = 2

                    R.id.tab_four ->
                        binding.viewPage.currentItem = 3

                    R.id.open_live ->
                        binding.viewPage.currentItem = 2
                }
            }
        }

        XXPermissions.with(this).permission(
            Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION
        ).request(object : OnPermissionCallback {

            override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                if (!allGranted) {
                    longToast("获取部分权限成功，但部分权限未正常授予")
                    return
                }
                initLocation()
            }

            override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                if (doNotAskAgain) {
                    longToast("被永久拒绝授权，请手动授予定位权限")
                    // 如果是被永久拒绝就跳转到应用权限系统设置页面
                    XXPermissions.startPermissionActivity(this@MainActivity, permissions)
                } else {
                    longToast("获取定位权限失败")
                }
            }
        })
    }


    private fun initLocation() {
        //声明定位回调监听器
        mLocationListener = AMapLocationListener() {
            viewModel.location.value = it
        }
        //初始化定位
        mLocationClient = AMapLocationClient(applicationContext).apply {
            //设置定位回调监听
            setLocationListener(mLocationListener)
            //声明AMapLocationClientOption对象
            var mLocationOption: AMapLocationClientOption? = null
            //初始化AMapLocationClientOption对象
            mLocationOption = AMapLocationClientOption().apply {
                locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy;
                isOnceLocation = true
                isOnceLocationLatest = true;
                //设置是否允许模拟位置,默认为true，允许模拟位置
                //isMockEnable = true
            }
            setLocationOption(mLocationOption);
            //给定位客户端对象设置定位参数
            setLocationOption(mLocationOption);
            //启动定位
            startLocation();
        }
    }

    private inner class ScreenSlidePagerAdapter(fa: FragmentManager, lc: Lifecycle) :
        FragmentStateAdapter(fa, lc) {

        val list = mutableListOf(
            PlazaFragment.newInstance(),
            NearFragment.newInstance(),
            PrepareFragment.newInstance()
        )

        override fun getItemCount(): Int = list.size

        override fun createFragment(position: Int): Fragment {
            return list[position]
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //释放定位
        mLocationClient?.unRegisterLocationListener(mLocationListener)
    }
}